﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Easy.BLL;
using Easy.Model;
using Easy.Operation;
using Easy.Web.Service.Data;

namespace Easy.Web.Pages.EntityPages
{
    public partial class EntityFormConfigPage : BasePage
    {
        public string Fields = "";
        public string entityname = "";
        public string entityid = "";
        public string formname = "";

        FormBaseBLL fbb;
        JsHubBLL JsHub=new JsHubBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            entityname = Request.Params["entityname"];
            entityid = Request.Params["Id"];
            formname = Request.Params["formname"] == null ? "" : Request.Params["formname"];
            fbb = new FormBaseBLL(entityname);

            Core c = new Core(Request.Params["entityname"]);

            foreach (KeyValuePair<string, Field> kvp in c.GetFields())
            {
                Fields += string.Format("<li {3} role='presentation' okey='{2}' atttype={1} class='active'><a href='#'>{0}</a></li>",
                    kvp.Value.DisplayName,
                    kvp.Value.Type,
                    kvp.Key,
                    string.Format("_innerHTML=\"{0}\"", Server.HtmlEncode(getHTML(kvp.Key, kvp.Value.DisplayName, kvp.Value.Type.ToString(), kvp.Value.LookupEntityName)))
                );
            }
            if (!IsPostBack)
            {
                initForm();
            }
        }
        private void initForm()
        {
            if (formname != "")
            {
                List<string> formStruncture = fbb.getFormStructure(formname);
                List<JsList> jslist = JsHub.GetAllJsList(entityname, formname, "form");
                JsList.Items.Clear();
                foreach (JsList jsList in jslist)
                {
                    var item = new ListItem(jsList.JsName, jsList.JsListId.ToString()) {Selected = jsList.JsCount > 0};
                    JsList.Items.Add(item);
                }
                if (formStruncture != null)
                {
                    content.Text = formStruncture[0];
                    span_formname.InnerText = formStruncture[1];
                    buttons.Value = formStruncture[2];
                    //buttonContent.InnerHtml = formStruncture[2];
                    //jscontent_Src.Text = formStruncture[3];
                    jscontent_beforeLoad.Text = formStruncture[4];
                    jscontent_afterLoad.Text = formStruncture[5];
                    jscontent_beforeSave.Text = formStruncture[6];
                    jscontent_afterSave.Text = formStruncture[7];
                }
            }
            else
            {
                span_formname.InnerText = "[创建]";
            }
        }

        private string getHTML(string id, string name, string type, string entity)
        {
            string reHTML = "";
            switch (type)
            {
                case "nvarchar":
                    reHTML = Templete.Str(id, name, type, false);
                    break;
                case "int":
                    reHTML = Templete.Int(id, name, type, false);
                    break;
                case "float":
                    reHTML = Templete.Float(id, name, type, false);
                    break;
                case "date":
                    reHTML = Templete.FormDate(id, name, type, false);
                    break;
                case "datetime":
                    reHTML = Templete.FormTime(id, name, type, false);
                    break;
                case "ntext":
                    reHTML = Templete.Ntext(id, name, type, false);
                    break;
                case "key":
                    reHTML = Templete.Str(id, name, type, false);
                    break;
                case "picklist":
                    reHTML = Templete.Picklist(id, name, type, false);
                    break;
                case "bit":
                    reHTML = Templete.Radio(id, name, type, false);
                    break;
                case "lookup":
                    reHTML = Templete.Lookup(id, name, type, entity, false);
                    break;
            }
            return reHTML;
        }

        protected void SaveFormStructure_Click(object sender, EventArgs e)
        {
            string formHtml = HttpUtility.HtmlDecode(content.Text);
            string formButton = HttpUtility.HtmlDecode(HttpUtility.UrlDecode(buttons.Value.Replace("+","%2B")));
            fbb.SaveFormStructure(formname, formHtml, formButton, "", jscontent_beforeLoad.Text, jscontent_afterLoad.Text, jscontent_beforeSave.Text, jscontent_afterSave.Text);
            var selected = JsList.GetSelectedIndices();
            List<Guid> lis = selected.Select(t => Guid.Parse(JsList.Items[t].Value)).ToList();
            JsHub.ModifyFormJsRelation(entityname, formname, lis);
            buttons.Value = formButton;
            content.Text = formHtml;

            Response.Redirect("/Pages/EntityPages/EntityFormConfigPage.aspx?Id=" + entityid + "&entityname=" + entityname + "&formname=" + formname, false);
        }

        protected void SaveFormStructure_Create_Click(object sender, EventArgs e)
        {
            string formHtml = HttpUtility.HtmlDecode(content.Text);
            fbb.CreateFormStructure(FormNameModalName.Value, FormNameModalDisplayName.Value, formHtml, "", jscontent_beforeLoad.Text, jscontent_afterLoad.Text, jscontent_beforeSave.Text, jscontent_afterSave.Text, GetSystemUser().SystemUserId.ToString());
            var selected = JsList.GetSelectedIndices();
            List<Guid> lis = selected.Select(t => Guid.Parse(JsList.Items[t].Value)).ToList();
            JsHub.ModifyFormJsRelation(entityname, formname, lis);
            content.Text = formHtml;
            Response.Redirect("/Pages/EntityPages/EntityFormConfigPage.aspx?Id=" + entityid + "&entityname=" + entityname + "&formname=" + FormNameModalName.Value, false);
        }
    }
    public static class Templete
    {
        public static string Lookup(string id, string name, string type, string entity, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
                 "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
                 "<div class='col-md-10'>\n" +
                       "<div class='input-group'>\n" +
                              "<input class='form-control' id='{0}' attname='{0}' type='text' atttype='{2}' etn='{3}' oid='' disabled>\n" +
                              "<span class='input-group-btn'>\n" +
                                    "<button class='btn btn-primary ng-binding btn-lookup' id='{0}_btn' fieldid='{0}' etn='{3}'  type='button'>\n" +
                                        "<span class='glyphicon glyphicon-search'></span>\n" +
                                    "</button>\n" +
                              "</span>\n" +
                       "</div>\n" +
                 "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type, entity);
        }
        public static string Str(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}' >\n" +
            "<!--**********begin_" + id + "**********-->\n" +
                "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
                "<div class='col-md-10'>\n" +
                    "<input type='input' class='form-control' id='{0}' attname='{0}' atttype='{2}' />\n" +
                "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string Picklist(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
                "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
                "<div class='col-md-10'>\n" +
                    "<select class='form-control' attname='{0}' id='{0}' atttype='{2}'></select>\n" +
                "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string FormDate(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-10'>\n" +
            "<input type='input' class='form-control form-date' id='{0}' attname='{0}' atttype='{2}' />\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);


        }
        public static string FormTime(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-10'>\n" +
            "<input type='input' class='form-control form-time' id='{0}' attname='{0}' atttype='{2}' />\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string Int(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-10'>\n" +
            "<input type='input' class='form-control' onblur='core.Form.CheckIntInput(this);' id='{0}' attname='{0}' atttype='{2}' />\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string Float(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-10'>\n" +
            "<input type='input' class='form-control' onblur='core.Form.CheckFloatInput(this);' id='{0}' attname='{0}' atttype='{2}' />\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string Ntext(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-12 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-1 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-11'>\n" +
            "<textarea rows='2' class='form-control' attname='{0}' id='{0}' atttype='{2}'></textarea>\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
        public static string Radio(string id, string name, string type, bool isnecessary)
        {
            string templete =
            "<div class='col-md-6 form-group' id='c_{0}' atttype='{2}'>\n" +
            "<!--**********begin_" + id + "**********-->\n" +
            "<label for='{0}' class='col-md-2 control-label' title='{1}' >{1}" + (isnecessary ? "<span style='color:red; '> *<span>" : "") + "</label>\n" +
            "<div class='col-md-10' id='{0}' attname='{0}' atttype='{2}'>\n" +
            "</div>\n" +
            "<!--**********end_" + id + "**********-->\n" +
            "</div>\n";
            return string.Format(templete, id, name, type);
        }
    }
}